var time = require("../../../utils/util.js");
var app = getApp({});
Page({
  data: {
    myPageId: 0,
    showPhotoImg: false,
    message: {},
    messageCp: {},
    imgName: ["厂房外景", "办公场所", "常规设施", "营业执照"],
    imgNameId: "",
    guanzhuLength: "",
    fensiLength: "",
  },
  // 加载信息
  onLoad: function() {
    var that = this;
    var uid = wx.getStorageSync("uid");
    var openid = wx.getStorageSync("openid");
    //我的主页
    wx.request({
      url: 'https://www.mamingwang.cn/api/index/index',
      data: {
        openid: openid,
        uid: ""
      },
      method: "post",
      success(res) {
        console.log(res);
        if (res.data.code == -2) {
          wx.navigateTo({
            url: '../login/register/register',
          })
          that.setData({
            code: res.data.code
          })
        } else {
          that.setData({
            message: res.data.message,
            guanzhuLength: res.data.message.guid,
            fensiLength: res.data.message.fuid,
          })
          //我的产品与文章
          wx.request({
            url: 'https://www.mamingwang.cn/api/index/message',
            data: {
              openid: openid
            },
            method: "post",
            success(res) {
              console.log(res);
              if (res.data != "") {
                //字符串
                for (var i = 0; i < res.data.message.length; i++) {
                  if (res.data.message[i].type == 2) {
                    if (res.data.message[i].content.length > 24) {
                      res.data.message[i].content = res.data.message[i].content.substring(0, 24) + '...'
                    }
                  }
                  //时间
                  let d = new Date(res.data.message[i].createtime.replace(/-/g, '/')).getTime();
                  res.data.message[i].createtime = time.timeHandle(d);
                }
                that.setData({
                  messageCp: res.data.message,
                })
              }
            }
          })
        }
      }
    })
  },
  onShow:function(){
    this.onLoad();
  },
  //分享
  share: function(e) {
    var openid = wx.getStorageSync("openid");
    wx.request({
      url: 'https://www.mamingwang.cn/api/xinxi/user_share',
      data: {
        openid: openid,
        uid: e.currentTarget.dataset.uid
      },
      method: "post",
      success(res) {
        console.log(res)
      }
    })
  },
  //关注
  goFollow: function() {
    wx.navigateTo({
      url: 'follwFans/follwFans?pageId=0',
    })
  },
  //粉丝
  goFans: function() {
    wx.navigateTo({
      url: 'follwFans/follwFans?pageId=1',
    })
  },
  //产品详情
  goSpecific: function(e) {
    wx.navigateTo({
      url: '../jutixiangqing/chanpinjuti/chanpin?mid=' + e.currentTarget.dataset.mid + "&uid=" + e.currentTarget.dataset.uid,
    })
  },
  //文章详情
  goWenZhang: function(e) {
    wx.navigateTo({
      url: '../jutixiangqing/wenzhangjuti/wenzhang?mid=' + e.currentTarget.dataset.mid + "&uid=" + e.currentTarget.dataset.uid,
    })
  },
  //安全
  goSecurity: function() {
    wx.navigateTo({
      url: 'security/security'
    })
  },
  // 设置
  goSetting: function() {
    wx.navigateTo({
      url: 'setting/setting'
    })
  },
  //认证
  renZhengState: function() {
    var that = this;
    var openid = wx.getStorageSync("openid");
    wx.request({
      url: 'https://www.mamingwang.cn/api/index/renzheng',
      data: {
        openid: openid
      },
      method: "post",
      success(res) {
        if (res.data.code == 0) {//第一次认证
          wx.navigateTo({
            url: 'pay/pay?name=' + that.data.message.uname + '&avaimg=' + that.data.message.avatars_img,
          })
        }else if(res.data.code == 4){//认证中
          wx.navigateTo({
            url: 'renZheng/renZhengIng/renZhengIng',
          })
        }else if(res.data.code == 1){//成功
          wx.navigateTo({
            url: 'renZheng/renZhengEd/renZhengEd',
          })
        }else if(res.data.code == 2){//修改
          wx.navigateTo({
            url: 'renZheng/renZhengNoPass/renZhengNoPass',
          })
        }
      }
    })
  },
  //点击切换
  goZhuYe: function() {
    var myPageId = 0;
    this.setData({
      myPageId: myPageId
    })
  },
  goChanPin: function() {
    var myPageId = 1;
    this.setData({
      myPageId: myPageId
    })
  },

  // 弹窗
  removetouchmove: function() {},
  showPhotoImg: function(e) {
    this.setData({
      showPhotoImg: !this.data.showPhotoImg,
      imgNameId: e.currentTarget.id
    })
  },
  //删除产品文章
  //
  delShanChu: function(e) {
    var that = this;
    var openid = wx.getStorageSync("openid");
    wx.request({
      url: 'https://www.mamingwang.cn/api/index/del',
      data: {
        mid: e.currentTarget.dataset.mid,
        openid: openid
      },
      method: "post",
      success(res) {
        for (var i = 0; i < that.data.messageCp.length; i++) {
          if (e.currentTarget.dataset.mid == that.data.messageCp[i].mid) {
            that.data.messageCp.splice(i, 1)
          }
        }
        that.setData({
          messageCp: that.data.messageCp
        })
      }
    })
  }
})